using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using AvaAI批量点评系统.Services;

namespace AvaAI批量点评系统.Models;

/// <summary>
/// 学生评语预览数据模型，支持实时编辑和重新生成
/// </summary>
public class StudentCommentPreview : INotifyPropertyChanged
{
    private string _studentName = string.Empty;
    private string _subject = string.Empty;
    private string _grade = string.Empty;
    private string _difficulty = string.Empty;
    private string _comment = string.Empty;
    private bool _isModified = false;
    private bool _isRegenerating = false;

    // 能力评语相关属性
    private StudentAbilityEvaluation _abilityEvaluation = new();

    /// <summary>
    /// 学生姓名
    /// </summary>
    public string StudentName
    {
        get => _studentName;
        set => SetProperty(ref _studentName, value);
    }

    /// <summary>
    /// 课程科目
    /// </summary>
    public string Subject
    {
        get => _subject;
        set => SetProperty(ref _subject, value);
    }

    /// <summary>
    /// 年级
    /// </summary>
    public string Grade
    {
        get => _grade;
        set => SetProperty(ref _grade, value);
    }

    /// <summary>
    /// 难度等级
    /// </summary>
    public string Difficulty
    {
        get => _difficulty;
        set => SetProperty(ref _difficulty, value);
    }

    /// <summary>
    /// 评语内容
    /// </summary>
    public string Comment
    {
        get => _comment;
        set
        {
            if (SetProperty(ref _comment, value))
            {
                IsModified = true;
            }
        }
    }

    /// <summary>
    /// 是否已被修改
    /// </summary>
    public bool IsModified
    {
        get => _isModified;
        set => SetProperty(ref _isModified, value);
    }

    /// <summary>
    /// 是否正在重新生成
    /// </summary>
    public bool IsRegenerating
    {
        get => _isRegenerating;
        set => SetProperty(ref _isRegenerating, value);
    }

    /// <summary>
    /// 能力评价数据
    /// </summary>
    public StudentAbilityEvaluation AbilityEvaluation
    {
        get => _abilityEvaluation;
        set => SetProperty(ref _abilityEvaluation, value);
    }

    /// <summary>
    /// 评语字数
    /// </summary>
    public int CommentLength => Comment?.Length ?? 0;

    /// <summary>
    /// 状态显示文本
    /// </summary>
    public string StatusText
    {
        get
        {
            if (IsRegenerating) return "🔄 重新生成中...";
            if (IsModified) return "✏️ 已修改";
            return "✅ 原始";
        }
    }

    public event PropertyChangedEventHandler? PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        
        // 当Comment变化时，通知相关属性也更新
        if (propertyName == nameof(Comment))
        {
            OnPropertyChanged(nameof(CommentLength));
        }
        
        // 当IsModified或IsRegenerating变化时，更新StatusText
        if (propertyName == nameof(IsModified) || propertyName == nameof(IsRegenerating))
        {
            OnPropertyChanged(nameof(StatusText));
        }
    }

    protected bool SetProperty<T>(ref T backingStore, T value, [CallerMemberName] string? propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(backingStore, value))
            return false;

        backingStore = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    /// <summary>
    /// 重置修改状态
    /// </summary>
    public void ResetModifiedStatus()
    {
        IsModified = false;
    }

    /// <summary>
    /// 从StudentComment创建预览对象
    /// </summary>
    public static StudentCommentPreview FromStudentComment(StudentComment comment)
    {
        var preview = new StudentCommentPreview
        {
            StudentName = comment.StudentName,
            Subject = comment.Subject,
            Grade = comment.StartGrade,
            Difficulty = comment.Difficulty,
            Comment = comment.Comment,
            IsModified = false
        };

        // 初始化能力评价数据，设置学生姓名
        preview.AbilityEvaluation.StudentName = comment.StudentName;

        return preview;
    }

    /// <summary>
    /// 转换为StudentComment对象
    /// </summary>
    public StudentComment ToStudentComment()
    {
        return new StudentComment
        {
            StudentName = StudentName,
            Subject = Subject,
            StartGrade = Grade,
            EndGrade = Grade,
            Difficulty = Difficulty,
            Comment = Comment,
            AbilityEvaluation = AbilityEvaluation
        };
    }
}
